package com.zego.wawaji.ui.store.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.store.bean.Store;
import com.zego.wawaji.wawaji_client.App;

import java.util.List;

/**
 * 兑换商品列表
 * Created by Su on 2018/1/4.
 */
public class StoreListAdapter extends RecyclerView.Adapter<StoreListAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<Store> mRoomList;

    public StoreListAdapter(Context context, List<Store> roomList) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        mRoomList = roomList;
    }

    public void setList(List<Store> roomList) {
        this.mRoomList = roomList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_store_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            final Store store = mRoomList.get(position);
            ImageLoader.loadCenterCrop(context, store.getImg(), holder.goodsImg);
            holder.store_title.setText(store.getTitle());
            holder.store_price.setText(String.valueOf(store.getWwCoinCz()));

            // "https://item.m.jd.com/product/2967929.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=CopyURL"
            holder.skip_jd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIShow.showWebActivity(App.getContext(), 3, store.getUrl());
                }
            });

            holder.skip_change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIShow.showStoreConvertActivity(App.getActivity(), store);
                }
            });
        } catch (Exception e) {
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView goodsImg;
        private RelativeLayout store_car;
        private TextView store_price, store_title, skip_jd, skip_change;

        public ViewHolder(final View itemView) {
            super(itemView);
            goodsImg = (ImageView) itemView.findViewById(R.id.goods_img);
            store_car = (RelativeLayout) itemView.findViewById(R.id.store_car);
            store_price = (TextView) itemView.findViewById(R.id.store_price);
            store_title = (TextView) itemView.findViewById(R.id.store_title);
            skip_jd = (TextView) itemView.findViewById(R.id.skip_jd);
            skip_change = (TextView) itemView.findViewById(R.id.skip_change);
        }
    }

    @Override
    public int getItemCount() {
        return mRoomList == null ? 0 : mRoomList.size();
    }
}